package com.itheima.reggie.common;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;


@Component
@Slf4j
public class MyMetaObjectHandler implements MetaObjectHandler {


    /**
     * 插入时自动填充
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {
      log.info("公共字段自动填充[insert]....");
      log.info(metaObject.toString());
      //设置自动填充   参数一：属性名    参数二：填充的值
        metaObject.setValue("createTime", LocalDateTime.now()); //设置创建时间
        metaObject.setValue("updateTime", LocalDateTime.now()); //设置修改时间
        metaObject.setValue("createUser",BaseContext.getCurrentId());    //设置创建人id
        metaObject.setValue("updateUser", BaseContext.getCurrentId());   //设置修改人id
    }

    /**
     * 修改时自动填充
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("公共字段自动填充[update]....");
        log.info(metaObject.toString());
        metaObject.setValue("updateTime", LocalDateTime.now()); //设置修改时间
        metaObject.setValue("updateUser", BaseContext.getCurrentId());   //设置修改人id

    }
}
